package com.fernfx.xingtan.common.verifycode;

import android.content.Context;
import android.text.TextUtils;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.OkHttpProxy;
import com.fernfx.xingtan.common.network.SJJNetworkProxy;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.MobileInfoUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/3.
 * 发送验证码
 */

public final class VerifyCodeSend implements IRequestCallback{
    private BaseActivity activity;
    private String type;
    public VerifyCodeSend(BaseActivity activity,String type){
        this.activity = activity;
        this.type = type;
    }

    public boolean send(String phoneNumber){
        if (!RegexUtil.isMobileNumber(phoneNumber)) {
            ToastUtil.showCentertoast(R.string.input_proper_pnumber);
            return false;
        }
        Map<String,Object> requestMap = Constant.RequestArgs.REQUEST_ARGS_MAP;
        requestMap.clear();
        requestMap.put("areaCode",Constant.RequestArgs.AREA_CODE);
        requestMap.put("mobile",phoneNumber);
        requestMap.put("imei", MobileInfoUtil.getIMEI(activity));
        requestMap.put("ip", MobileInfoUtil.getIP(activity));
        requestMap.put("os", Constant.RequestArgs.SYSTEM_TYPE);
        requestMap.put("type",type);

        //map convert json
        String requestText = FastJsonUtil.BeanToJson(requestMap);
        //show loadding
        activity.showLoading();
        //clear map
        requestMap.clear();
        SJJNetworkProxy.getInstance().post(Constant.RequestArgs.PHONE_NUMBER_URL,null,requestText,this);
        return true;
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        //关闭加载框
        activity.hideLoading();

        String message = result.getMessage();
        BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
        if (OtherUtil.checkRequestStatus(baseEntity.getSuccess(),baseEntity.getStatus())) {
            ToastUtil.showCentertoast("发送成功");
        }else {
            ToastUtil.showCentertoast("发送失败");
        }
    }

    @Override
    public void onFailure(String errorMessage, int errorCode) {
        activity.hideLoading();
        if (TextUtils.isEmpty(errorMessage)) {
            onNetworkError(errorMessage,errorCode);
            return;
        }
        BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
        if (TextUtils.isEmpty(errorMessage)) {
            onNetworkError(errorMessage,errorCode);
        }else {
            ToastUtil.showCentertoast(baseEntity.getMsg());
        }
    }

    @Override
    public void onNetworkError(String errorMessage, int errorCode) {
        //关闭加载框
        activity.hideLoading();

        ToastUtil.showCentertoast(R.string.network_error);
    }
}
